package com.ddx.service.impl;

import com.ddx.common.redis.RedisKey;
import com.ddx.common.redis.RedisService;
import com.ddx.common.util.DataUtil;
import com.ddx.service.ICreateSerialNumService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2018/4/23
 */
@Service
public class CreateSerialNumServiceImpl implements ICreateSerialNumService {
    private static Logger logger = LoggerFactory.getLogger(CreateSerialNumServiceImpl.class);
    @Autowired
    RedisService redisService;

    /**
     * 商城订单号 规则userId+商品id+时间戳+自定义8位
     * @param userId
     * @param goodsId
     * @return
     */
    @Override
    public  String createSerialNum(String userId, String goodsId) {
        synchronized (this){
            String serialNum=userId;
            if(goodsId!=null){
                serialNum=serialNum+goodsId;
            }
            serialNum=serialNum+System.currentTimeMillis();
            Integer redisNum=0;
            if(redisService.get(RedisKey.SERIAL_NUMBER)==null){
                redisService.set(RedisKey.SERIAL_NUMBER,redisNum);
            }else {
                redisNum=Integer.parseInt(redisService.get(RedisKey.SERIAL_NUMBER).toString());
                redisNum=redisNum+1;
                redisService.set(RedisKey.SERIAL_NUMBER,redisNum);
            }
            serialNum=serialNum+ DataUtil.addZeroStr(redisNum.toString(),8);
            return serialNum;
        }
    }

    public static void main(String[] args) {
//        Jedis jedis = new Jedis();
//        jedis.set("name", "JackGSmith");
//        System.out.println("Hello World");
    }
}
